package com.biyao.service.model.report.imp;

import com.biyao.schedulingentity.base.TBearingBoxEntity;
import com.biyao.schedulingentity.base.TBearingBoxTypeEntity;
import com.biyao.schedulingentity.base.TRollerBasicEntity;
import com.biyao.schedulingentity.base.TRollerTypeEntity;
import com.biyao.service.model.base.TBearingBoxMessage;
import com.biyao.service.model.base.TBearingBoxTypeMessage;
import com.biyao.service.model.base.TRollerBasicMessage;
import com.biyao.service.model.base.TRollerTypeMessage;
import com.biyao.service.model.base.imp.TBearingService;
import com.biyao.service.model.report.RollerReportMessage;
import com.biyao.service.model.state.GrindingLogMessage;
import com.biyao.service.model.state.RollerExtMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RollerReportImp implements RollerReportMessage {

    @Autowired
    private RollerExtMessage rollerExtMessage;
    @Autowired
    private TRollerBasicMessage tRollerBasicMessage;
    @Autowired
    private TRollerTypeMessage tRollerTypeMessage;
    @Autowired
    private GrindingLogMessage grindingLogMessage;
    @Autowired
    private TBearingBoxMessage tBearingBoxMessage;
    @Autowired
    private TBearingBoxTypeMessage tBearingBoxTypeMessage;
    @Autowired
    private TBearingService tBearingService;

    @Override
    public Map selectRollerProfile() {
        Map backMap=new HashMap();
        List<Map> mapList=rollerExtMessage.selectRollerExtMessage();
        //低于废弃直径
        int lower=0;
        //距离废弃直径小于20
        int lower20=0;
        //距离废弃直径20-50
        int twentyToFifty=0;
        //距离废弃直径大于50
        int large50=0;
        if(mapList.size()>0){
            for(Map map:mapList){
                Float nowDiameter=(Float)map.get("diameter");
                Float discardDiameter=(Float)map.get("discard_diameter");
                if(nowDiameter!=null){
                    //当前直径低于废弃直径
                    if(nowDiameter<discardDiameter){
                        lower=lower+1;
                    }else if(nowDiameter<discardDiameter+20){
                        lower20=lower20+1;
                    }else if(discardDiameter+20<=nowDiameter&&nowDiameter<=discardDiameter+50){
                        twentyToFifty=twentyToFifty+1;
                    }else if(discardDiameter+50<nowDiameter){
                        large50=large50+1;
                    }

                }

            }
        }
        backMap.put("lower",lower);
        backMap.put("lower20",lower20);
        backMap.put("twentyToFifty",twentyToFifty);
        backMap.put("large50",large50);
        return backMap;
    }

    @Override
    public List<Map> selectBearingBox() {
       List<Map> list= tBearingBoxMessage.selectBearingBoxList();
       if(list!=null&&list.size()>0){
           for(Map map:list){
               Integer id=(Integer)map.get("roller_new_id");
               if(id!=null){
                   Map t= rollerExtMessage.selectRollerExtByRollerId(id);
                   map.put("parRollerCode",t.get("parRollerCode"));
                   map.put("operateBox",t.get("operateBox"));
                   map.put("operateBear",t.get("operateBear"));
                   map.put("tranBox",t.get("tranBox"));
                   map.put("tranBear",t.get("tranBear"));
               }
           }

       }
       return list;
    }

    @Override
    public List<Map> selectBearing() {
        return tBearingService.selectBearingReport();
    }

    @Override
    public List<Map> selectBearingBoxDetail() {
        return null;
    }


    public List<Map> getRollerDetailReport(){
        List<Map> backList=new ArrayList<>();
        List<Map> rollerStateList=tRollerBasicMessage.selectRollerBasicList();
        for(Map map:rollerStateList){
            Map rollerDetail=new HashMap();
            Integer rollerId=(Integer) map.get("id");
            TRollerBasicEntity tRollerBasicEntity=tRollerBasicMessage.selectRollerBasicById(rollerId);
            rollerDetail.put("rollerCode",tRollerBasicEntity.getCode());//轧辊编号
            if(tRollerBasicEntity.getTypeId()!=null){
                TRollerTypeEntity rollerTypeEntity=tRollerTypeMessage.selectTRollerTypeById(tRollerBasicEntity.getTypeId());
                rollerDetail.put("rollerType",rollerTypeEntity.getCode());//轧辊类型
            }
            Float originalDiameter=tRollerBasicEntity.getOriginalDiameter();//初始直径
            Float discardDiameter=tRollerBasicEntity.getDiscardDiameter();//报销直径
            Map grindData=grindingLogMessage.selectGrinderToRollerDetailReport(rollerId);
            Integer dis=(Integer) grindData.get("dis");
            String firstTime=(String)grindData.get("firstTime");
            Integer time=(Integer)grindData.get("time");
            Float allRemoveNumber=(Float)grindData.get("allRemoveNumber");
            if(originalDiameter!=null){
                if(allRemoveNumber==null){
                    allRemoveNumber=0f;
                }
                rollerDetail.put("nowDia",originalDiameter-allRemoveNumber);//当前直径
            }
            rollerDetail.put("discardDia",discardDiameter);//报废直径
            rollerDetail.put("firstTime",firstTime);//首次磨削时间
            rollerDetail.put("dia",dis);//轧制里程
            rollerDetail.put("allTime",time);//总磨削时间
            rollerDetail.put("id",rollerId);
            backList.add(rollerDetail);
        }
        return backList;
    }
    @Override
    public List<Map> selectRollerDetail() {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        List<Map> rollerStateList=rollerExtMessage.selectNowState();
        List<Map> backList=new ArrayList<>();
        for(Map map:rollerStateList){
            Map bearingBox=new HashMap();
            Integer rollerId=(Integer) map.get("rollerId");
            TRollerBasicEntity tRollerBasicEntity=tRollerBasicMessage.selectRollerBasicById(rollerId);
            if(tRollerBasicEntity.getCode()!=null){
                TBearingBoxEntity tBearingBoxEntity=tBearingBoxMessage.selectBearingBoxByCode(tRollerBasicEntity.getCode());
                if(tBearingBoxEntity!=null){
                    bearingBox.put("code",tBearingBoxEntity.getCode());
                }
                if(tBearingBoxEntity.getMaintenanceDate()!=null){
                    bearingBox.put("maintenanceDate",simpleDateFormat.format(tBearingBoxEntity.getMaintenanceDate()));
                }
                if(tBearingBoxEntity.getBearingBoxTypeId()!=null){
                    TBearingBoxTypeEntity   tBearingBoxTypeEntity=tBearingBoxTypeMessage.selectBearingBox(tBearingBoxEntity.getBearingBoxTypeId());
                    bearingBox.put("type",tBearingBoxTypeEntity.getCode());
                }
            }
            Map grindData=grindingLogMessage.selectGrinderToRollerDetailReport(rollerId);
            String firstTime=(String)grindData.get("firstTime");
            Integer dis=(Integer) grindData.get("dis");
            Date endTime=(Date)grindData.get("endTime");
            bearingBox.put("firstTime",firstTime);
            bearingBox.put("dia",dis);
            bearingBox.put("endTime",getNextMonth(endTime));
            bearingBox.put("rollerCode",tRollerBasicEntity.getCode());
            backList.add(bearingBox);

        }
        return backList;
    }

    public String getNextMonth(Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH,1);
        return simpleDateFormat.format(calendar.getTime());
    }
}
